<template>
  <div>
    <Form label-position="right" :label-width="400" :model="formData">
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">身份证号码：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.idcardno" placeholder="请输入身份证号码"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">联系电话：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.mobile" placeholder="请输入联系电话"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">申请确认人：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.applicantConfirmer" placeholder="请输入申请确认人"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">户籍所在地：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.regPlace" placeholder="请输入户籍所在地"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">居住地址：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.realAddress" placeholder="请输入居住地址"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">籍贯：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.nativePlace" placeholder="请输入籍贯"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">收入来源：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.sourceRevenue">
            <Option v-for="item in sourceRevenueList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
        <Col span="5" class="ivu-form-item-label">收入水平：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.incomeLevel">
            <Option v-for="item in incomeLevelList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">婚姻状况：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.marriageStatus">
            <Option v-for="item in marriageStatusList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
        <Col span="5" class="ivu-form-item-label">居住状况：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.dwellingCondition">
            <Option v-for="item in dwellingConditionList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">子女情况：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.thing">
            <Option v-for="item in thingList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
        <Col span="5" class="ivu-form-item-label">所属人群：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.subsidiestype" @on-change="subsidystandardsValue">
            <Option v-for="item in subsidiestypeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">失能失智自评等级：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.disabilityAlzheimer" @on-change="subsidystandardsValue">
            <Option v-for="item in disabilityAlzheimerList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
        <Col span="5" class="ivu-form-item-label">申请补贴标准：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.subsidystandards" disabled>
            <Option v-for="item in subsidystandardsList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">家属姓名：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.bailorname" placeholder="请输入家属姓名"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">与申请人关系：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.applicantrelationship" >
            <Option v-for="item in applicantrelationshipList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">家属地址：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.principaladdress" placeholder="请输入家属地址"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">家属联系方式：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.principalphone" placeholder="请输入家属联系方式"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">申请人员类型：</Col>
        <Col span="17" class="ivu-form-item-content">
          <Select v-model="formData.differentiate">
            <Option v-for="item in differentiateList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
      </Row>
    </Form>
  </div>
</template>

<script>
import { getInPensionState, getDisabilityCareState, getFamilyPlanningState } from '@/api/eventlist'
export default {
  props: {
    formData2: Object, // 上传数据
    cardno: String,
    personinfo: Object // 人员信息
  },
  name: 'purchaseHomeCare',
  data () {
    return {
      selectData: '',
      sectionList: [],
      sectionListb: [],
      sectionListc: [],
      formData: { // 上传数据
        idcardno: '',
        applicantConfirmer: '',
        sexDictId: '',
        age: '',
        birthday: '',
        regPlace: '',
        mobile: '',
        realAddress: '',
        nativePlace: ''
      },
      saveState: true,
      sourceRevenueList: [
        { value: '1', label: '离退休金' },
        { value: '2', label: '劳动工资' },
        { value: '3', label: '其他' },
        { value: '4', label: '无收入' }
      ],
      incomeLevelList: [
        { value: '1', label: '1000元以下' },
        { value: '2', label: '1000-2000元' },
        { value: '3', label: '2000-4000元' },
        { value: '4', label: '4000元以上' }
      ],
      marriageStatusList: [
        { value: '1', label: '已婚' },
        { value: '2', label: '未婚' }
      ],
      dwellingConditionList: [
        { value: '1', label: '独居' },
        { value: '2', label: '空巢' },
        { value: '3', label: '子女同住' }
      ],
      thingList: [
        { value: '1', label: '无子女' },
        { value: '2', label: '有独生子女' },
        { value: '3', label: '有两个以上独生子女' }
      ],
      subsidiestypeList: [
        { value: '1', label: '低保' },
        { value: '2', label: '低保边缘' },
        { value: '3', label: '计划生育特殊家庭' },
        { value: '4', label: '分散供养特困人员' },
        { value: '5', label: '80周岁以上高龄老人' }
      ],
      disabilityAlzheimerList: [
        { value: '1', label: '身体智力正常' },
        { value: '2', label: '失能' },
        { value: '3', label: '半失能' },
        { value: '4', label: '失智' }
      ],
      applicantrelationshipList: [
        { value: '0', label: '本人' },
        { value: '1', label: '子女' },
        { value: '2', label: '近亲属' },
        { value: '3', label: '其他' }
      ],
      subsidystandardsList: [
        { value: '1', label: '45小时/月' },
        { value: '2', label: '30小时/月' },
        { value: '3', label: '3小时/月' }
      ],
      differentiateList: [
        { value: 1, label: '关爱式人员申请' },
        { value: 2, label: '普惠式人员申请' }
      ]
    }
  },
  watch: {
    // 回显
    formData2 (a) {
      if (a.idcardno !== undefined) {
        this.formData = a
      } else {
        console.log(this.personinfo, '政府购买服务')
        this.formData.applicantConfirmer = this.personinfo.name // 姓名
        this.formData.idcardno = this.cardno // 身份证
        this.formData.sexDictId = this.cardno.substr(16, 1) % 2 === 1 ? '1' : '2'
        this.formData.birthday = this.cardno.substr(6, 4) + '-' + this.cardno.substr(10, 2) + '-' + this.cardno.substr(12, 2) // 出生日期
        this.formData.age = this.getAge(this.cardno) // 年龄
        this.formData.regPlace = this.personinfo.domicile_address || '' // 户籍地址
        this.formData.realAddress = this.personinfo.address || '' // 籍贯
        this.formData.nativePlace = this.personinfo.present_address || '' // 现住址
        this.formData.bailorname = this.$route.params.GUARD_NAME // 监护人姓名
        this.formData.principalphone = this.$route.params.GUARD_PHONE // 监护人电话
        this.formData.mobile = ''
      }
    }
  },
  methods: {
    getAge (idcardno) {
      let birthday = (idcardno.substr(6, 4) + '-' + idcardno.substr(10, 2) + '-' + idcardno.substr(12, 2)) // 出生日期
      let birthdays = new Date(birthday.replace(/-/g, '/'))
      let d = new Date()
      let age =
        d.getFullYear() -
        birthdays.getFullYear() -
        (d.getMonth() < birthdays.getMonth() ||
        (d.getMonth() === birthdays.getMonth() &&
          d.getDate() < birthdays.getDate()) ? 1 : 0)
      return age
    },
    subsidystandardsValue () {
      this.formData.subsidystandards = ''
      let age = this.getAge(this.formData.idcardno)
      console.log()
      let subsidiestype = this.formData.subsidiestype
      if (this.formData.subsidiestype === '3') {
        getFamilyPlanningState({ 'idcardno': this.formData.idcardno }).then(res => {
          if (res.data.errcode === 0) {
            console.log(res)
            if (!res.data.data.success) {
              console.log(res.data)
              this.$Notice.error({ title: '信息不符合', desc: '该老人不属于计划生育特殊家庭' })
              return false
            }
          } else {
            this.$Notice.error({ title: '查询计划生育人员信息失败', desc: res.data.date.message })
          }
        })
      }
      let disabilityAlzheimer = this.formData.disabilityAlzheimer
      if (subsidiestype === '5' && (disabilityAlzheimer === '1' || disabilityAlzheimer === '2' || disabilityAlzheimer === '3' || disabilityAlzheimer === '4')) {
        this.formData.subsidystandards = '3'
      } else if (age >= 70 && subsidiestype === '3' && disabilityAlzheimer === '1') {
        this.formData.subsidystandards = '3'
      } else if ((subsidiestype === '1' || subsidiestype === '2' || subsidiestype === '3' || subsidiestype === '4') && disabilityAlzheimer === '1') {
        this.$Notice.error({ title: '不符合申报条件，无法享受此服务', desc: '' })
      } else if ((subsidiestype === '1' || subsidiestype === '2' || subsidiestype === '3' || subsidiestype === '4') && disabilityAlzheimer === '3') {
        this.formData.subsidystandards = '2'
      } else if ((subsidiestype === '1' || subsidiestype === '2' || subsidiestype === '3' || subsidiestype === '4') && (disabilityAlzheimer === '2' || disabilityAlzheimer === '4')) {
        this.formData.subsidystandards = '1'
      }
    },
    test () {
      let reg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/
      let reg1 = /^1[3456789]\d{9}$/
      if (reg.test(this.formData.idcardno) === false) {
        this.$Notice.error({ title: '身份证不合法', desc: '' })
        return false
      } else if (reg1.test(this.formData.mobile) === false) {
        this.$Notice.error({ title: '联系方式不合法', desc: '' })
        return false
      } else if (this.formData.applicantConfirmer === '' || this.formData.applicantConfirmer === undefined) {
        this.$Notice.error({ title: '请输入申请确认人', desc: '' })
        return false
      } else if (this.formData.regPlace === '' || this.formData.regPlace === undefined) {
        this.$Notice.error({ title: '请输入户籍所在地', desc: '' })
        return false
      } else if (this.formData.realAddress === '' || this.formData.realAddress === undefined) {
        this.$Notice.error({ title: '请输入居住地址', desc: '' })
        return false
      } else if (this.formData.nativePlace === '' || this.formData.nativePlace === undefined) {
        this.$Notice.error({ title: '请输入籍贯', desc: '' })
        return false
      } else if (this.formData.sourceRevenue === '' || this.formData.sourceRevenue === undefined) {
        this.$Notice.error({ title: '请选择收入来源', desc: '' })
        return false
      } else if (this.formData.incomeLevel === '' || this.formData.incomeLevel === undefined) {
        this.$Notice.error({ title: '请选择收入水平', desc: '' })
        return false
      } else if (this.formData.marriageStatus === '' || this.formData.marriageStatus === undefined) {
        this.$Notice.error({ title: '请选择婚姻状况', desc: '' })
        return false
      } else if (this.formData.dwellingCondition === '' || this.formData.dwellingCondition === undefined) {
        this.$Notice.error({ title: '请选择居住状况', desc: '' })
        return false
      } else if (this.formData.thing === '' || this.formData.thing === undefined) {
        this.$Notice.error({ title: '请选择子女状况', desc: '' })
        return false
      } else if (this.formData.subsidiestype === '' || this.formData.subsidiestype === undefined) {
        this.$Notice.error({ title: '请选择所属人群', desc: '' })
        return false
      } else if (this.formData.disabilityAlzheimer === '' || this.formData.disabilityAlzheimer === undefined) {
        this.$Notice.error({ title: '请选择失能失智自评等级', desc: '' })
        return false
      } else if (this.formData.subsidystandards === '' || this.formData.subsidystandards === undefined) {
        this.$Notice.error({ title: '请选择申请补贴标准', desc: '' })
        return false
      } else if (this.formData.bailorname === '' || this.formData.bailorname === undefined) {
        this.$Notice.error({ title: '请输入家属姓名', desc: '' })
        return false
      } else if (this.formData.applicantrelationship === '' || this.formData.applicantrelationship === undefined) {
        this.$Notice.error({ title: '请输入与申请人关系', desc: '' })
        return false
      } else if (this.formData.principaladdress === '' || this.formData.principaladdress === undefined) {
        this.$Notice.error({ title: '请输入家属地址', desc: '' })
        return false
      } else if (reg1.test(this.formData.principalphone) === false) {
        this.$Notice.error({ title: '家属联系方式不合法', desc: '' })
        return false
      } else if (this.formData.differentiate === '' || this.formData.differentiate === undefined) {
        this.$Notice.error({ title: '请选择申请人类型', desc: '' })
        return false
      } else if (this.formData.subsidiesType === '3') {
        getFamilyPlanningState({ 'idcardno': this.formData.idcardno }).then(res => {
          if (res.data.errcode === 0) {
            if (!res.data.data.success) {
              this.$Notice.error({ title: '信息不符合', desc: '该老人不属于计划生育特殊家庭' })
              return false
            }
          } else {
            this.$Notice.error({ title: '查询计划生育人员信息失败', desc: res.data.date.message })
          }
        })
        return false
      } else if (this.formData.idcardno !== '' || this.formData.idcardno !== undefined) {
        // 机构在养人员信息查询
        getInPensionState({ 'idcardno': this.formData.idcardno }).then(res => {
          if (res.data.errcode === 0) {
            if (res.data.data.success) {
              this.$Notice.error({ title: '不符合申报条件，无法享受此服务', desc: '该老人已是机构在养人员' })
              return false
            }
          } else {
            this.$Notice.error({ title: '查询机构在养人员信息失败', desc: res.data.date.message })
          }
        })
        // 失能护理补贴人员信息查询
        getDisabilityCareState({ 'idcardno': this.formData.idcardno }).then(res => {
          if (res.data.errcode === 0) {
            if (res.data.data.success) {
              this.$Notice.error({ title: '保存失败', desc: '该老人已是失能护理补贴人员' })
              return false
            }
          } else {
            this.$Notice.error({ title: '查询失能护理补贴人员信息失败', desc: res.data.date.message })
          }
        })
        this.$emit('purchaseHome', this.formData)
        this.$Message.success('表单保存成功')
      } else {
        this.$emit('purchaseHome', this.formData)
        this.$Message.success('表单保存成功')
      }
    }
  },
  mounted () {
  }
}
</script>

<style scoped>
  .ivu-row{
    margin-bottom: 15px;
  }
</style>
